//
//  YHKLoginViewController.swift
//  xinlangweiBo2
//
//  Created by 杨和逵 on 16/2/28.
//  Copyright © 2016年 yanghekui. All rights reserved.
//

import UIKit
import AFNetworking

class YHKLoginViewController: UIViewController,UIWebViewDelegate {

    var webView:UIWebView?
    
    override func loadView() {
        
        //创建UIWebView
      webView = UIWebView()
        
        view = webView
        
        //设置代理
        webView?.delegate = self
        
    }
    
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        

        
       let url = NSURL(string: "https://api.weibo.com/oauth2/authorize?client_id=\(App_Key)&redirect_uri=\(App_URL)&response_type=code")!
        
        let requset = NSURLRequest(URL:url)
        
        webView?.loadRequest(requset)
        
        
        
        //颜色
        view.backgroundColor = UIColor.whiteColor()

        setUp()
        
    }
    
    
    //创建两个btn
    func setUp(){
        
        navigationItem.leftBarButtonItem = UIBarButtonItem(title: "返回", style: UIBarButtonItemStyle.Plain, target: self, action: "returnClick")
        navigationItem.rightBarButtonItem = UIBarButtonItem(title: "自动填充", style: UIBarButtonItemStyle.Plain, target: self, action: "autocompleteClick")
        
    }
    
    
    //上面两个按钮的点击事件
    func returnClick(){
        
        dismissViewControllerAnimated(true) { () -> Void in
            
        }
        
        
    }
    
    
    func autocompleteClick(){
        
        
          let js = "document.getElementById(\"userId\").value = \"18679166956\";document.getElementById(\"passwd\").value = \"1993kuibinjin.\";"
        
        webView?.stringByEvaluatingJavaScriptFromString(js)
    }
  

}




extension YHKLoginViewController{
    
//MARK ===webView的代理方法
    
    func webViewDidStartLoad(webView: UIWebView) {}
    
    func webViewDidFinishLoad(webView: UIWebView) {}
    
    
    func webView(webView: UIWebView, didFailLoadWithError error: NSError?) {}
    
    
    
    //这个方法可以获取我们想要的url
    func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool {
     
        if let url = request.URL?.absoluteString{
            
//            print(url)
        
            if url.hasPrefix(App_URL){
                
                if let code = request.URL?.query{
                    
                    let subCode = code.substringFromIndex("code=".endIndex)
//                    print(subCode)
                    
                    //拿到了code数据之后发送一个网络请求Token
                  YHKLoginModelView.shareInstance.loadToken(subCode, duiBlock: { () -> () in
                    print("成功")
                    
                    //发布一个通知
                    NSNotificationCenter.defaultCenter().postNotificationName(kNotificaionChangeRootVC, object: nil)
                    
                    //销毁通知
                    self.dismissViewControllerAnimated(true, completion: { () -> Void in
                        
                    })
                    
                    
                    }, cuoBloc: { () -> () in
                        print("失败")
                  })
                
                    
                }
            }
        }
        return true
        
    }

    
}

//MARK: 通过code来获取Tocken
extension YHKLoginViewController{
    
//    https://api.weibo.com/oauth2/access_token
    

}



//MARK:通过Toekn来获取个人信息
extension YHKLoginViewController{
    
    /**
    
    必选	类型及范围	说明
    access_token	true	string	采用OAuth授权方式为必填参数，OAuth授权后获得。
    uid	false	int64	需要查询的用户ID。
    screen_name	false	string	需要查询的用户昵称。
    
    参数uid与screen_name二者必选其一，且只能选其一；
    
    */
    
    

    
    
}


